package com.ruoyi.system.controller;

import com.ruoyi.system.service.ISmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Author YangLi
 * @Date 2024/9/11 18:17
 * @注释
 */
@RestController
@RequestMapping
@Slf4j
public class CwSmsController {

    @Resource
    private ISmsService smsService;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping("/send-sms-code")
    public String sendCode(@RequestParam String phoneNumber) {
        // 生成随机验证码
        String code = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));

        // 将验证码发送给用户
        boolean success = smsService.sendVerificationCode(phoneNumber, code);

        if (success) {
            // 你可以将验证码存储在Redis中，并设置过期时间
            redisTemplate.opsForValue().set("SMS_" + phoneNumber, code, 5, TimeUnit.MINUTES);
            return "验证码发送成功";
        } else {
            return "验证码发送失败";
        }
    }
}